<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//| 
//+-----------------------------------------------------------


namespace app\common\plugins;


use think\facade\Cache;
use think\facade\Event;

/**
 * 插件服务注册器
 * Class Service
 * @package app\common\plugins
 */
class Service extends \think\Service
{
    protected $pluginPath = '';

    public function register(): void
    {
        $this->pluginPath = $this->getPluginPath();
        //注册事件
        $this->loadEvent();
        $this->app->bind('plugin', Service::class);
    }

    public function boot()
    {
        $this->registerRoutes(function (\think\Route $route) {
            $execute = '\\app\\common\\plugins\\Route::execute';
            if (is_file($this->app->plugin->getPluginPath() . 'middleware.php')) {
                $this->app->middleware->import(include $this->app->plugin->getPluginsPath() . 'middleware.php', 'route');
            }

            $route->rule('plugins/:plugin/[:module]/[:controller]/[:action]', $execute)->middleware(Middleware::class);
            $files = glob($this->getPluginPath() . '*/route/*.php');
            foreach ($files as $file) {
                $routes = include $file;
                if (empty($routes)) {
                    continue;
                }

                foreach ($routes as $key => $value) {
                    if (!$value) continue;
                    if (is_array($value)) {
                        $domain = $value['domain'];
                        $rules = [];
                        foreach ($value['rule'] as $k => $rule) {
                            [$plugin, $module, $controller, $action] = explode('/', $rule);
                            $rules[$k] = [
                                'plugin' => $plugin,
                                'module' => $module,
                                'controller' => $controller,
                                'action' => $action,
                                'indomain' => 1,
                            ];
                        }
                        $route->domain($domain, function () use ($rules, $route, $execute) {
                            // 动态注册域名的路由规则
                            foreach ($rules as $k => $rule) {
                                $route->rule($k, $execute)
                                    ->name($k)
                                    ->completeMatch(true)
                                    ->append($rule);
                            }
                        });
                    } else {
                        list($plugin, $module, $controller, $action) = explode('/', $value);
                        $route->rule($key, $execute)
                            ->name($key)
                            ->completeMatch(true)
                            ->append([
                                'plugin' => $plugin,
                                'module' => $module,
                                'controller' => $controller,
                                'action' => $action
                            ]);
                    }
                }
            }
        });
    }

    protected function getPluginPath()
    {
        return app()->getRootPath() . 'plugins' . DIRECTORY_SEPARATOR;
    }

    protected function loadEvent()
    {
        $events = $this->app->isDebug() ? [] : Cache::get('event', []);
        if (empty($events)) {
            /**
             * @var Array $bind
             * @var Array $listen
             * @var Array $subscribe
             */
            $events = include $this->pluginPath . 'event.php';
            Cache::set('event', $events);
        }
        Event::listenEvents($events);
    }
}